<?php

namespace App\Http\Middleware;

use App\Models\Users;
use Closure;
use Illuminate\Support\Arr;

class CheckViewsSession
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(empty(session('loginUser'))){
            return redirect('/');
        }
        $user = Users::where('id' , Arr::get(session('loginUser') , 'id'))->first();

        if(!$user){
            session(['loginUser' => null]);
            return redirect('/');
        }
        return $next($request);
    }
}
